package com.westvalley.service.api.nc;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.kit.StringKit;
import com.westvalley.http.HttpXmlClient;

public class BudgetExceedService extends NcService{
	public static final String KEY = "budgetExceed";
	public static final JsonType jsonType = JsonType.arr;
	private Controller ctrl;
	
	public static BudgetExceedService me(Controller ctrl){
		return new BudgetExceedService(ctrl);
	}
	
	private BudgetExceedService(Controller ctrl){
		super(KEY,jsonType);
		this.ctrl = ctrl;
	}
	
	@Override
	protected String executeInterface(Map<String, Object> params) {
		String bodyParam = JSON.toJSONString(params);
		return HttpXmlClient.postBody(this.url, bodyParam, "application/json");
	}
	
	@Override
	protected Map<String, Object> initParams() {
		Map<String,Object> map = Maps.newHashMap();
		//费用承担公司
		Record unitinfo = Db.findFirst("SELECT * FROM WV_NC_UNIT WHERE PK_CORP = ?",this.ctrl.getPara("costunit"));
		if(unitinfo!=null)
			map.put("unitcode", StringKit.null2o(unitinfo.get("UNITCODE")));	//公司编码
		//费用承担部门
		Record deptinfo = Db.findFirst("SELECT * FROM WV_NC_DEPT WHERE PK_DEPTDOC = ?",this.ctrl.getPara("costdept"));
		if(deptinfo!=null)
			map.put("deptcode", StringKit.null2o(deptinfo.get("DEPTCODE")));	//部门编码
		map.put("billdate", StringKit.null2Str(ctrl.getPara("billdate")));		//单据日期
		map.put("billtype", StringKit.null2Str(ctrl.getPara("billtype")));		//交易类型
		JSONArray arr = JSON.parseArray(ctrl.getPara("costItems"));				
		map.put("ysobj", arr);													//费用明细
		return map;
	}

}
